<template>
  <el-dialog v-model="drawer" :append-to-body="true" width="600px" title="语言能力" @close="close">
    <el-form class="refForm" ref="ruleRefForm" label-width="100px" :model="languageForm" size="small">
      <!--        <div v-for="(item, index) in form.list" :key="item.id">-->
      <el-row>
        <!-- :prop="`list.${index}.language` -->
        <el-form-item label="语种" prop="name" :rules="{ required: true, message: '请输入语种', trigger: 'blur' }">
          <el-input v-model="languageForm.name" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="掌握程度" prop="proficiency" :rules="{ required: true, message: '请输入掌握程度', trigger: 'blur' }">
          <el-input v-model="languageForm.proficiency" placeholder="请输入"></el-input>
        </el-form-item>
      </el-row>
      <el-form-item label="备注" prop="remark">
        <el-input type="textarea" v-model="languageForm.remark" placeholder="请输入" style="width:420px"></el-input>
      </el-form-item>
      <!--            <el-divider v-if="index < form.list.length - 1" style="margin: 0 0 18px" content-position="right">-->
      <!--                <el-button size="small" plain type="primary" link @click="delForm(index)">删除</el-button>-->
      <!--            </el-divider>-->
      <!--        </div>-->
    </el-form>
    <div class="footer-fixed">
      <!--        <el-row align="middle" class="add-fixed" @click="addLanguage">-->
      <!--            <el-icon color="var(&#45;&#45;el-color-primary-dark-2)"><ele-Plus /></el-icon>-->
      <!--            <span class="add-tips">新增语言能力</span>-->
      <!--        </el-row>-->
      <el-row justify="center">
        <el-button type="primary" size="small" @click="submit(ruleRefForm)">保存</el-button>
        <el-button size="small" @click="resetForm(ruleRefForm)">取消</el-button>
      </el-row>
    </div>
  </el-dialog>
</template>

<script>
import {computed, reactive, toRefs, ref} from 'vue'
import {ElMessage} from 'element-plus'
import {createLanguage, editLanguage} from '/@/api/oa/staff/language'

export default {
  name: 'LanguageDetail',
  props: {
    dialogDrawer: {
      type: Boolean,
      default: false
    },
    staffLanguage: {
      type: Object,
      default: {}
    }
  },
  setup(props, context) {
    const {dialogDrawer} = toRefs(props)
    const {staffLanguage} = toRefs(props)
    const ruleRefForm = ref()
    const drawer = computed({
      get: () => dialogDrawer.value,
      set: val => {
        context.emit('update:dialogDrawer', val)
      }
    })
    const formData = computed({
      get: () => staffLanguage.value,
      set: val => {
        context.emit('update:staffLanguage', val)
      }
    })
    const state = reactive({
      languageForm: formData
    })
    const close = () => {
      context.emit('update:dialogDrawer', false)
    }
    // const addLanguage = () => {
    //   state.form.list.push({})
    // }
    // const delForm = index => {
    //   state.form.list.splice(index, 1)
    // }
    const submit = async (formEl) => {
      if (!formEl) return
      await formEl.validate(async (valid) => {
        if (valid) {
          try {
            if (state.languageForm.id) {
              await editLanguage(state.languageForm)
            } else {
              await createLanguage(state.languageForm)
            }
            ElMessage.success('保存成功')
            context.emit('update:dialogDrawer', false)
          } catch (e) {
            console.log(e)
          }
        }
      })
    }
    const resetForm = (formEl) => {
      if (!formEl) return
      context.emit('update:dialogDrawer', false)
      formEl.resetFields()
    }
    return {
      drawer,
      formData,
      close,
      // addLanguage,
      // delForm,
      submit,
      resetForm,
      ruleRefForm,
      ...toRefs(state)
    }
  }
}
</script>

<style scoped lang="scss">
:deep(.el-form-item) {
  align-items: flex-start;
}

.refForm {
  padding-bottom: 80px;
}

.footer-fixed {
  position: absolute;
  left: 0;
  bottom: 0;
  right: 0;
  padding: 10px 0;
  background: #fff;
  z-index: 999;
  box-shadow: rgba(0, 0, 0, 0.1) 0px 0px 5px 0px, rgba(0, 0, 0, 0.1) 0px 0px 1px 0px;

  .add-fixed {
    padding-left: 20px;
    color: var(--el-color-primary);
    cursor: pointer;
    border-bottom: 1px dotted var(--el-border-color-light);
    padding-bottom: 10px;
    margin-bottom: 10px;

    .add-tips {
      margin-left: 5px;
      font-size: 13px;
    }
  }
}
</style>